package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.mission.schedule.R;
import com.mission.schedule.activity.CalenderNoteEditActivity;
import com.mission.schedule.adapter.ChangYongYuAdapter;
import com.mission.schedule.adapter.DaoJiShiTiXingAdapter;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.CYTXBean;
import com.mission.schedule.clock.QueryAlarmData;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.entity.ScheduleTable;
import com.mission.schedule.my160920.picker.TimePicker2;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.LineGridView;
import com.mission.schedule.utils.SharedPrefUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 常用提醒详情对话框
 * Created by liwei on 2017/4/18.
 */

public class ReminderDetailDialog extends Dialog {


    CallBack callBack;

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    /**
     *
     * @param context
     * @param themeResId
     * @param windowManager
     * @param Date
     * @param Time
     * @param TimeType 类型
     * @param C 时间差
     * @param Content
     */
    public ReminderDetailDialog(@NonNull final Context context, @StyleRes final int themeResId, final WindowManager windowManager,
                                final String Date, final String Time, final String TimeType, final String C, String Content) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;

        setCanceledOnTouchOutside(true);

        View view = LayoutInflater.from(context).inflate(R.layout.reminder_detail_dialog,null);
        setContentView(view);

        final TextView content = (TextView) view.findViewById(R.id.content);
        content.setText(Content);

        TextView time = (TextView) view.findViewById(R.id.time);
        time.setText(Date + " " + Time +" " + C + TimeType +"后");

        view.findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TimeType.equals("分钟")) {
                    App.getDBcApplication().insertScheduleData(
                            Time + "分钟倒计时提醒", Date, Time, 1, 0, 1, 0, 0, 0, 0,
                            DateUtil.formatDateTimeSs(new Date()), "", 0, "", "", 0, "", "", 1, 0, 0,
                            "到点提醒", "g_001", "", 0, 0, 0, "", "", 0, 0, 0,0,"","0","0");
                    QueryAlarmData.writeAlarm(context);

                } else if (TimeType.equals("小时")) {
                    App.getDBcApplication().insertScheduleData(
                            "起床提醒", Date, Time, 1, 0, 1, 0, 0,
                            0, 0, DateUtil.formatDateTimeSs(new Date()),
                            "", 0, "", "", 0, "", "", 1, 0, 0,
                            "起床", "g_202", "", 0, 0, 0, "", "", 0, 0, 0,0,"","0","0");
                    QueryAlarmData.writeAlarm(context);
                }
                dismiss();
            }
        });

        view.findViewById(R.id.code).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    int schID = App.getDBcApplication().getScheduleTableMinId() - 1;
                    if (TimeType.equals("分钟")) {
                        App.getDBcApplication().insertScheduleData(
                                Time + "分钟倒计时提醒", Date, Time, 1, 0, 1, 0, 0, 0, 0,
                                DateUtil.formatDateTimeSs(new Date()), "", 0, "", "", 0, "", "", 1, 0, 0,
                                "到点提醒", "g_001", "", 0, 0, 0, "", "", 0, 0, 0,0,"","0","0");
                        QueryAlarmData.writeAlarm(context);

                    } else if (TimeType.equals("小时")) {
                        App.getDBcApplication().insertScheduleData(
                                "起床提醒", Date, Time, 1, 0, 1, 0, 0,
                                0, 0, DateUtil.formatDateTimeSs(new Date()),
                                "", 0, "", "", 0, "", "", 1, 0, 0,
                                "起床", "g_202", "", 0, 0, 0, "", "", 0, 0, 0,0,"","0","0");
                        QueryAlarmData.writeAlarm(context);
                    }
                    Map<String, String> map = App.getDBcApplication().querayOneCalenderData(schID);
                    Intent cintent = new Intent(context, CalenderNoteEditActivity.class);
                    cintent.putExtra("lingshengnames", map.get(ScheduleTable.schRingDesc));
                    cintent.putExtra("lingshengcode", map.get(ScheduleTable.schRingCode));
                    cintent.putExtra("schTime", map.get(ScheduleTable.schTime));
                    cintent.putExtra("schDate", map.get(ScheduleTable.schDate));
                    cintent.putExtra("schIsAlarm", map.get(ScheduleTable.schIsAlarm));
                    cintent.putExtra("schBeforeTime", map.get(ScheduleTable.schBeforeTime));
                    cintent.putExtra("schDisplayTime", map.get(ScheduleTable.schDisplayTime));
                    cintent.putExtra("schIsPostpone", map.get(ScheduleTable.schIsPostpone));
                    cintent.putExtra("content", map.get(ScheduleTable.schContent));
                    cintent.putExtra("schSourceDesc", map.get(ScheduleTable.schSourceDesc));
                    cintent.putExtra("schImagePath", map.get(ScheduleTable.schImagePath));
                    cintent.putExtra("schID", map.get(ScheduleTable.schID));
                    cintent.putExtra("schIsEnd", map.get(ScheduleTable.schIsEnd));
                    cintent.putExtra("schRepeatID", map.get(ScheduleTable.schRepeatID));
                    context.startActivity(cintent);
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(context, "操作错误,请重试", Toast.LENGTH_SHORT).show();
                }
                dismiss();
            }
        });

        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth() - 30;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }


    public interface CallBack{}

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }


}
